package com.bytz.modules.cms.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.order.model.OrderBill;
import com.bytz.modules.cms.order.model.SendBillRequest;
import com.bytz.modules.cms.order.service.IOrderBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags = "账单")
@RequestMapping("/bill")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderBill.EDIT + "')")
public class OrderBillController {

    @Autowired
    private IOrderBillService billService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<OrderBill>> page(OrderBill orderBill,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) throws Exception {

        return Result.ok(billService.queryPage(new Page<>(pageNo, pageSize), orderBill, req.getParameterMap()));
    }

    @ApiOperation(value = "查询一条", notes = "查询一条")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<OrderBill> page(@RequestParam String id) throws Exception {

        return Result.ok(billService.queryById(id));
    }

    @ApiOperation(value = "发送选中日账单给财务", notes = "发送选中日账单给财务")
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Result<Boolean> sendDailyBill(@RequestBody SendBillRequest request) throws IOException {
        billService.sendDailyBill(request.getDate());
        return Result.ok();
    }

    @ApiOperation(value = "导出列表", notes = "导出列表")
    @RequestMapping(value = "/exportXls", method = RequestMethod.POST)
    public void exportXls(OrderBill orderBill, HttpServletRequest req, HttpServletResponse response) throws Exception {
        billService.exportXls(response, orderBill, req.getParameterMap());
    }

}
